package org.dolphin.entity.measurement;

import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.util.ObjectUtils;

@Entity
@Table(name = "XS_MEASUREMENT_CONVERSION")
@AssociationOverrides(value = {
    @AssociationOverride(name = "pk.source", joinColumns = @JoinColumn(name = "UOM_SOURCE_ID")),
    @AssociationOverride(name = "pk.target", joinColumns = @JoinColumn(name = "UOM_TARGET_ID"))})
public class MeasurementConversion implements Serializable {
  private static final long serialVersionUID = 7673695330944064338L;
  private MeasurementConversionPk pk;
  private BigDecimal value;

  public MeasurementConversion() {
    this(new MeasurementConversionPk());
  }

  public MeasurementConversion(MeasurementConversionPk pk) {
    this.pk = pk;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    } else if (obj instanceof MeasurementConversion) {
      MeasurementConversion oth = (MeasurementConversion) obj;
      return ObjectUtils.nullSafeEquals(getPk(), oth.getPk());
    }
    return false;
  }

  @EmbeddedId
  public MeasurementConversionPk getPk() {
    return pk;
  }

  @Transient
  public UnitOfMeasurement getSource() {
    return pk.getSource();
  }

  @Transient
  public UnitOfMeasurement getTarget() {
    return pk.getTarget();
  }

  @Column(name = "CONVERSION_VALUE", nullable = false, precision = 10, scale = 5)
  public BigDecimal getValue() {
    return value;
  }

  @Override
  public int hashCode() {
    int hash = ObjectUtils.nullSafeHashCode(getPk());
    return hash;
  }

  public void setPk(MeasurementConversionPk pk) {
    this.pk = pk;
  }

  public void setSource(UnitOfMeasurement source) {
    this.getPk().setSource(source);
  }

  public void setTarget(UnitOfMeasurement target) {
    this.getPk().setTarget(target);
  }

  public void setValue(BigDecimal value) {
    this.value = value;
  }
}
